package com.server.common.feign.config;

import com.server.common.code.error.BusinessException;
import com.server.common.security.code.thread.ThreadLocalHeaderUtils;
import feign.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

/**
 * feign 请求拦截器
 * 
 */
@Component
@Slf4j
@ConditionalOnClass({RequestInterceptor.class})
public class FeignRequestInterceptor implements RequestInterceptor
{

    @Override
    public void apply(RequestTemplate requestTemplate)
    {
        //只有网址路径包含private才可以访问
        String url = requestTemplate.url();

        if(!url.contains("/private/")){
            throw new BusinessException("无法访问");
        }

        for (String key : ThreadLocalHeaderUtils.getMap().keySet()){
            requestTemplate.header(key, ThreadLocalHeaderUtils.getHeader(key));
        }
    }


}